package com.atguigu.gmall.map;



/**
 * @author 陈昆昆
 * @version 1.0
 * @date 2020/6/12 23:32
 */
public class MyHashMap<K,V> {
    private Entry<K,V>[] table;
    public final int  CAPUT = 2;
    private int size;

    public void put(K key,V value){

        if (table ==null){
            init();
        }
        int hashcode = hash(key);
        int index = bacuket(hashcode);

        for( Entry<K,V> entry =table[index]; entry!=null; entry = entry.next){
            if (entry.getKey().equals(key)){
                entry.key=key;
            }
        }
        addEntry(key,value,index);
    }
    public V get(K key){
        int hashcode = hash(key);
        int index = bacuket(hashcode);
        for( Entry<K,V> entry =table[index]; entry!=null; entry = entry.next){
            if (entry.getKey().equals(key)){
               return entry.getValue();
            }
        }
        return null;
    }

    public static void main(String[] args) {
        MyHashMap<String,String> map = new MyHashMap<>();
        map.put("1","11");
        map.put("1","33");
        map.put("2","22");

    }
    private void addEntry(K key, V value, int index) {
        Entry<K, V> kvEntry = new Entry<>(key,value,table[index]);
        table[index]=kvEntry;
        size++;
    }

    private int bacuket(int hashcode) {
        return hashcode%CAPUT;
    }

    public int hash(K key){

        return key.hashCode();
    }

    public void init(){
        table=new Entry[CAPUT];
    }



    class Entry<K,V>{
        public K key;
        public V value;
        public Entry<K,V> next;

        public K getKey() {
            return key;
        }
        public V getValue(){
            return value;
        }

        public Entry(K key, V value, Entry<K, V> next) {
            this.key = key;
            this.value = value;
            this.next = next;
        }
        public Entry(K key, V value) {
            this.key = key;
            this.value = value;
        }


    }
}
